package com.letv.thrift;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeoutException;

import serving.ResultDocInfo;

import com.letv.so.service.impl.ByteTranscoder;
import com.letv.so.util.StringUtil;

import net.rubyeye.xmemcached.KeyIterator;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.transcoders.Transcoder;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
 * Simple example for xmemcached
 * 
 * @author tanlu
 * 
 * 
 */
public class SimpleExample {
	public static void main(String[] args) throws UnsupportedEncodingException {
		if (args.length < 1) {
			System.err.println("Useage:object-key");
			System.exit(1);
		}
		String address = "127.0.0.1:11211"; // Cbase Address
		MemcachedClient memcachedClient = getMemcachedClient(address);
		if (memcachedClient == null) {
			throw new NullPointerException("Null MemcachedClient,please check memcached has been started");
		}
		try {
			// add a,b,c
			// System.out.println("Add a,b,c");
			// memcachedClient.set("a", 0, "Hello,xmemcached");
			// memcachedClient.set("b", 0, "Hello,xmemcached");
			// memcachedClient.set("c", 0, "Hello,xmemcached");

			// get object
			String value = memcachedClient.get(args[0]);
			System.out.println("get object=" + value);

			ResultDocInfo docInfo;
			// System.out.println("charset:" + getEncoding(value));//
			// 目前看到的值都是UTF-8
			docInfo = StringUtil.byte2ResultDocInfo(value.getBytes("UTF-8"));
			System.out.print(docInfo);
			// System.out.print(docInfo.getVideo_attribute().getExternal_id());

			// System.out.println("delete a");
			// // delete a
			// memcachedClient.delete("a");
			// // reget a
			// value = memcachedClient.get("a");
			// System.out.println("after delete,a=" + value);
			//
			// System.out.println("Iterate all keys...");
			// iterate all keys
			// KeyIterator it = memcachedClient.getKeyIterator(AddrUtil
			// .getOneAddress(args[0]));
			// while (it.hasNext()) {
			// System.out.println(it.next());
			// }
			// System.out.println(memcachedClient.touch("b", 1000));

		} catch (MemcachedException e) {
			System.err.println("MemcachedClient operation fail");
			e.printStackTrace();
		} catch (TimeoutException e) {
			System.err.println("MemcachedClient operation timeout");
			e.printStackTrace();
		} catch (InterruptedException e) {
			// ignore
		}
		try {
			memcachedClient.shutdown();
		} catch (Exception e) {
			System.err.println("Shutdown MemcachedClient fail");
			e.printStackTrace();
		}
	}

	public static MemcachedClient getMemcachedClient(String servers) {
		try {
			// use text protocol by default
			MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(servers));
			Transcoder paramTranscoder = new ByteTranscoder();
			builder.setTranscoder(paramTranscoder);
			return builder.build();
		} catch (IOException e) {
			System.err.println("Create MemcachedClient fail");
			e.printStackTrace();
		}
		return null;
	}

	public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s = encode;
				return s;
			}
		} catch (Exception exception) {
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s1 = encode;
				return s1;
			}
		} catch (Exception exception1) {
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s2 = encode;
				return s2;
			}
		} catch (Exception exception2) {
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s3 = encode;
				return s3;
			}
		} catch (Exception exception3) {
		}
		return "";
	}
}
